<template>
  <div class="home-container" ref="container" @wheel="handleWheel" v-loading="loading">
    <ul @transitionend="handleTransitionEnd" class="carousel-container" :style="{marginTop: marginTop}">
      <li v-for="item in data" :key="item.id">
        <Carouselitem :carousel="item"/>
      </li>
    </ul>
    <div v-show="index >= 1" class="icon icon-up" @click="switchTo(index - 1)">
      <Icon type="arrowUp" />
    </div>
    <div v-show="index < data.length - 1" class="icon icon-down" @click="switchTo(index + 1)">
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li @click="switchTo(i)" :class="{active: index === i}" v-for="(item, i) in data" :key="item.id"></li>
    </ul>
  </div>
</template>

<script>
import Carouselitem from "./Carouselitem.vue";
import Icon from "@/components/Iconfont";
import {mapState} from "vuex";

export default {
  components: {
    Carouselitem,
    Icon,
  },
  data() {
    return {
      index: 0,
      containerHeight: 0,
      switching: false, // 是否在切换中
    };
  },
  created() {
    this.$store.dispatch("banner/fetchBanner");
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.handleResize);
  },
  destroyed() {
    window.removeEventListener("resize", this.handleResize);
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
    ...mapState("banner", ["loading", "data"])
  },
  methods: {
    switchTo(i) {
      if(i < 0) {
        i = 0;
      }
      if(i > this.data.length - 1) {
        i = this.data.length - 1
      }
      this.index = i;
    },
    handleWheel(e) {
      if(this.switching) {
        return;
      }
      if(e.deltaY < 0 && this.index > 0) {
        this.switching = true;
        this.index--;
      } else if(e.deltaY > 0 && this.index < this.data.length - 1){
        this.switching = true;
        this.index++;
      }
    },
    handleTransitionEnd() {
      this.switching = false;
    },
    handleResize() {
      this.containerHeight = this.$refs.container.clientHeight;
    },
  },
};
</script>

<style lang="less" scoped>
@import "@/styles/mixin.less";
@import "@/styles/var.less";

.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  // 测试
}
.icon {
  .sel-center();
  transform: translateX(-50%);
  .iconfont {
    font-size: 30px;
    color: @gray;
    cursor: pointer;
  }
  &.icon-up {
    top: 25px;
    animation: jump-up 2s infinite;
  }
  &.icon-down {
    top: auto;
    bottom: 25px;
    animation: jump-down 2s infinite;
  }
  @jump: 5px;
  @keyframes jump-up {
    0% {
      transform: translate(-50%, @jump);
    }
    50% {
      transform: translate(-50%, -@jump);
    }
    100% {
      transform: translate(-50%, @jump);
    }
  }
  @keyframes jump-down {
    0% {
      transform: translate(-50%, -@jump);
    }
    50% {
      transform: translate(-50%, @jump);
    }
    100% {
      transform: translate(-50%, -@jump);
    }
  }
}
.indicator {
  .sel-center();
  transform: translateY(-50%);
  left: auto;
  right: 20px;
  li{
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: @words;
    cursor: pointer;
    margin-bottom: 10px;
    border: 1px solid #fff;
    box-sizing: border-box;
    transition: 0.5s;
    &.active {
      background: #fff;
    }
  }
}
.carousel-container{
  width: 100%;
  height: 100%;
  transition: all 0.5s;
  li{
    width: 100%;
    height: 100%;
  }
}
</style>